package net.atomarrow.monitor;

import java.io.File;
import java.io.FileFilter;

import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;

/**
 * 类变动监视器
 * @author ZhangYi
 *
 */
public class ClassAlterationMonitor {
	private FileAlterationMonitor monitor;
	
	public void start(String path){
		FileAlterationObserver observer=new FileAlterationObserver(path,new FileFilter() {
			//设定监控文件策略
			@Override
			public boolean accept(File file) {
                return (!file.isFile() || file.getName().endsWith("class"))
                        && !file.getName().contains("$");
            }
		} );
		observer.addListener(new DefaultFileAlteratinListener());
		//每三秒扫描一次
		monitor=new FileAlterationMonitor(1000, observer);
		try {
			monitor.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void stop(){
		if(monitor!=null){
			try {
				monitor.stop();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
